package pl.nazaweb.samplesandtutorials;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ButtonExample extends Activity {

	ToggleButton toggleButton;
	CheckBox firstCheckBox;
	RadioGroup radioGroup;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.button_example);
		toggleButton = (ToggleButton) findViewById(R.id.toggle_button);
		toggleButton.setOnClickListener(getToggleButtonOnClickListener());

		firstCheckBox = (CheckBox) findViewById(R.id.checkbox_1);
		firstCheckBox.toggle(); // zmiana stanu checkboxa
		firstCheckBox
				.setOnCheckedChangeListener(getOnCheckBoxCheckedChangeListener());
		
		radioGroup = (RadioGroup)findViewById(R.id.radio_group);
		radioGroup.setOnCheckedChangeListener(getRadioGroupOnCheckedChangeListener());
	}

	private RadioGroup.OnCheckedChangeListener getRadioGroupOnCheckedChangeListener() {
		return new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case -1:
					showToast("Wyczyszczono wybrane elementy");
					break;
				case R.id.radio_button_1:
					showToast("Wybrano pierwszy radio button");
					break;
				case R.id.radio_button_2:
					showToast("Wybrano drugi radio button");
					break;
				case R.id.radio_button_3:
					showToast("Wybrano trzeci radio button");
					break;
				default:
					break;
				}
			}
		};
	}

	private CompoundButton.OnCheckedChangeListener getOnCheckBoxCheckedChangeListener() {
		return new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				showToast("CheckBox pierwszy jest : "
						+ (isChecked ? "wybrany" : "niewybrany"));
			}
		};

	}

	private OnClickListener getToggleButtonOnClickListener() {
		return new OnClickListener() {
			public void onClick(View v) {
				if (toggleButton.isChecked()) {
					showToast("Toggle button is on!");
				} else {
					showToast("Toggle button is off");
				}
			}
		};
	}

	private void showToast(String message) {
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.button_example, menu);
		return true;
	}
}
